import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DickYang
 * Date: 2023-06-23
 * Time: 17:47
 */
public class MyArrayList {
    public int[] elem;
    public int usedSize;
    public static int DEFAULT_SIZE = 10;

    public MyArrayList(){
        this.elem = new int[DEFAULT_SIZE];
    }

    // 打印顺序表，注意：该方法并不是顺序表中的方法，为了方便看测试结果给出的
    public void display() {
        System.out.print("[");
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i]);
            if (i != this.usedSize-1) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return true;
            }
        }
         return false;
    }

    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return i;
            }
        }
        return -1; 
    }
    // 获取 pos 位置的元素
    public int get(int pos) {
        inspectGetIndex(pos);
        return this.elem[pos];
    }
    //判断Get位置的合法性
    public void inspectGetIndex(int pos) {
        if(pos < 0 || pos >= this.usedSize) {
            throw new IndexOutOfException("位置不合法!");
        }
    }
    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if(isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }
    //判断数组是否满了
    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        //检查POS位置的合法性
        inspectAddIndex(pos);
        if(isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        int last = this.usedSize-1;
        for (int i = pos; i <= last;) {
            this.elem[last+1] = this.elem[last];
            last--;
        }
        this.elem[pos] = data;
        usedSize++;
    }
    //判断add中POS位置的合法性
    public void inspectAddIndex(int pos) {
        if(pos < 0 || pos > this.elem.length) {
            throw new IndexOutOfException("位置不合法!");
        }
    }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        inspectSetIndex(pos);
        this.elem[pos] = value;
    }
    public void inspectSetIndex(int pos) {
        if(pos < 0 || pos > this.elem.length) {
            throw new IndexOutOfException("位置不合法!");
        }
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        if (contains(toRemove)) {
            for (int i = indexOf(toRemove); i < this.usedSize-1; i++) {
                this.elem[i] = this.elem[i+1];
            }
            this.usedSize--;
            this.elem[usedSize] = 0;
        } else {
            System.out.println("没有这个数!");
        }
    }
    // 清空顺序表
    public void clear() {
        for (int i = 0; i < this.usedSize; i++) {
            this.elem[i] = 0;
        }
        this.usedSize = 0;
    }

}
